package com.thb.pay.payment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.api.payment.CurrencyTypeServiceApi;
import com.thb.pay.api.payment.vo.req.CurrencyTypePageReq;
import com.thb.pay.api.payment.vo.req.CurrencyTypeReq;
import com.thb.pay.api.payment.vo.resp.CurrencyTypeVo;
import com.thb.pay.core.exception.RRException;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.entity.SuperEntity;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.payment.entity.CurrencyType;
import com.thb.pay.payment.service.CurrencyTypeService;
import com.xiaoleilu.hutool.bean.BeanUtil;
import com.xiaoleilu.hutool.collection.CollUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 货币-类型表 前端控制器
 * </p>
 *
 * @author lucky
 * @since 2019-10-23
 */
@Slf4j
@RestController
public class CurrencyTypeController implements CurrencyTypeServiceApi {

    @Autowired
    private CurrencyTypeService currencyTypeService;

    @Override
    public PageResult<CurrencyTypeVo> findPage(@RequestBody CurrencyTypePageReq pageReq) {
        IPage<CurrencyType> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<CurrencyType> pageResult = currencyTypeService.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, CurrencyTypeVo.class);
    }

    @Override
    public List<CurrencyTypeVo> findAllList() {
        List<CurrencyTypeVo> resultDataList = Lists.newArrayList();
        List<CurrencyType> targetDbResultList = currencyTypeService.selectList(null);
        targetDbResultList.forEach(dbResult -> {
            CurrencyTypeVo resultData = new CurrencyTypeVo();
            BeanUtil.copyProperties(dbResult, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public List<CurrencyTypeVo> findListByIds(@RequestBody List<Long> ids) {
        List<CurrencyTypeVo> resultDataList = Lists.newArrayList();
        List<CurrencyType> resDataList = currencyTypeService.selectBatchIds(ids);
        resDataList.forEach(payProduct -> {
            CurrencyTypeVo resultData = new CurrencyTypeVo();
            BeanUtil.copyProperties(payProduct, resultData);
            resultDataList.add(resultData);
        });
        return resultDataList;
    }

    @Override
    public CurrencyTypeVo getById(@RequestParam("id") Long id) {
        CurrencyTypeVo resultData = null;
        CurrencyType dbResult = currencyTypeService.selectById(id);
        if (dbResult != null) {
            resultData = new CurrencyTypeVo();
            BeanUtil.copyProperties(dbResult, resultData);
        }
        return resultData;
    }

    @Override
    public CurrencyTypeVo getByCode(@RequestParam("code") String code) {
        CurrencyTypeVo resultData = null;
        LambdaQueryWrapper<CurrencyType> wrapper = new LambdaQueryWrapper();
        wrapper.eq(CurrencyType::getCode, code);
        List<CurrencyType> targetDbResultList = currencyTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            resultData = new CurrencyTypeVo();
            BeanUtil.copyProperties(targetDbResultList.get(0), resultData);
        }
        return resultData;
    }

    @Override
    public void add(@RequestBody CurrencyTypeReq currencyTypeReq) {
        LambdaQueryWrapper<CurrencyType> wrapper = new LambdaQueryWrapper();
        wrapper.eq(CurrencyType::getCode, currencyTypeReq.getCode());
        List<CurrencyType> targetDbResultList = currencyTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            throw RRException.build("currency.type.exist");
        }
        CurrencyType currencyType = new CurrencyType();
        BeanUtil.copyProperties(currencyTypeReq, currencyType);
        currencyTypeService.insert(currencyType);
    }

    @Override
    public void update(@RequestBody CurrencyTypeReq currencyTypeReq) {
        CurrencyType currencyType = currencyTypeService.getById(currencyTypeReq.getId());
        if (currencyType == null) {
            throw RRException.build("currency.type.not.exist");
        }
        if (!currencyType.getCode().equals(currencyTypeReq.getCode())) {
            LambdaQueryWrapper<CurrencyType> wrapper = new LambdaQueryWrapper();
            wrapper.eq(CurrencyType::getCode, currencyTypeReq.getCode());
            List<CurrencyType> targetDbResultList = currencyTypeService.selectList(wrapper);
            if (CollUtil.isNotEmpty(targetDbResultList)) {
                throw RRException.build("currency.type.exist");
            }
        }
        CurrencyType targetCurrencyType = new CurrencyType();
        BeanUtil.copyProperties(currencyTypeReq, targetCurrencyType);
        currencyTypeService.updateById(targetCurrencyType);
    }

    @Override
    public void deleteBatchIds(@RequestBody List<Long> ids) {
        LambdaQueryWrapper<CurrencyType> wrapper = new LambdaQueryWrapper();
        wrapper.in(CurrencyType::getId, ids);
        List<CurrencyType> targetDbResultList = currencyTypeService.selectList(wrapper);
        if (CollUtil.isNotEmpty(targetDbResultList)) {
            targetDbResultList.forEach(payType -> {
                payType.setDelFlag(SuperEntity.DEFAULT_DELETE_FLAG);
            });
            currencyTypeService.updateBatchById(targetDbResultList);
        }
    }
}
